﻿using System;

namespace Wwtfly.Utility
{
    /// <summary>
    /// 数据类型转换
    /// </summary>
    public static class ConvertHelper {


        /// <summary>
        /// 转换成 double
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="isSuccess">转换成是否成功</param>
        /// <returns>double</returns>
        public static double AsDouble(string s, out bool isSuccess)
        {
            double result;
            return (isSuccess = double.TryParse(s, out result)) ? result : 0;
        }
        /// <summary>
        /// 转换成 bool
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>Boolean</returns>
        public static Boolean AsBool(this String s, bool defaultValue = false) {
            bool result;
            return Boolean.TryParse(s, out result) ? result : defaultValue;
        }
        /// <summary>
        /// 转换成 bool
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>Boolean</returns>
        public static Boolean? AsBoolOfNull(this String s, bool? defaultValue = null)
        {
            bool result;
            return Boolean.TryParse(s, out result) ? result : defaultValue;
        }

        /// <summary>
        /// 转换成 datetime
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>DateTime</returns>
        public static DateTime AsDateTime(this String s, DateTime defaultValue = new DateTime()) {
            DateTime result;
            return DateTime.TryParse(s, out result) ? result : defaultValue;
        }

        /// <summary>
        /// 转换成 dateTime
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="isSuccess">转换成是否成功</param>
        /// <returns>double</returns>
        public static DateTime AsDateTime(string s, out bool isSuccess)
        {
            DateTime result;
            isSuccess = DateTime.TryParse(s, out result);
            return result;
        }

        /// <summary>
        /// 转换成 Decimal
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>Decimal</returns>
        public static Decimal AsDecimal(this String s, Decimal defaultValue = 0M) {
            Decimal result;
            return Decimal.TryParse(s, out result) ? result : defaultValue;
        }
        /// <summary>
        /// 转换成 float
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>float</returns>
        public static float AsFloat(this String s, float defaultValue = 0f) {
            float result;
            return float.TryParse(s, out result) ? result : defaultValue;
        }
        /// <summary>
        /// 转换成 Int32
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>Int32</returns>
        public static Int32 AsInt(this String s, int defaultValue = 0) {
            int result = defaultValue;
            return int.TryParse(s, out result) ? result : defaultValue;
        }
        /// <summary>
        /// 日期转换成 String
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>string</returns>
        public static string DateToString(this DateTime d)
        {
            return d.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 日期转换成 String
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>string</returns>
        public static string DateToString(this DateTime? d)
        {
            return DateToString(d, "yyyy-MM-dd");
        }

        /// <summary>
        /// 日期转换成 String
        /// </summary>
        /// <param name="s">要转换的参数</param>
        /// <param name="defaultValue">转换失败缺省值</param>
        /// <returns>string</returns>
        public static string DateToString(this DateTime? d,string format)
        {
            return d.HasValue ? d.Value.ToString(format) : string.Empty;
        }
        /// <summary>
        /// 可空类型转换
        /// </summary>
        /// <typeparam name="T">转换的可空类型</typeparam>
        /// <param name="s">字符串</param>
        /// <returns>转换的可空类型</returns>
        public static T AsNullable<T>(this String s) {
            if (string.IsNullOrEmpty(s)) {
                return default(T);
            }
            else {
                try {
                    return (T)Convert.ChangeType(s, typeof(T).GetGenericArguments()[0]);
                }
                catch {
                    return default(T);
                }
            }
        }

    }
}
